﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace testCam
{
    public class iWearDllBridge
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct IWRMagSensor
        {
            [MarshalAs(UnmanagedType.U1)]
            public byte magx_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte magx_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte magy_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte magy_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte magz_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte magz_msb;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct IWRAccelSensor
        {
            [MarshalAs(UnmanagedType.U1)]
            public byte accx_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte accx_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte accy_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte accy_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte accz_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte accz_msb;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct IWRGyroSensor
        {
            [MarshalAs(UnmanagedType.U1)]
            public byte gyx_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyx_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyy_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyy_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyz_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyz_msb;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct IWRLBGyroSensor
        {
            [MarshalAs(UnmanagedType.U1)]
            public byte gyx_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyx_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyy_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyy_msb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyz_lsb;
            [MarshalAs(UnmanagedType.U1)]
            public byte gyz_msb;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct IWRSENSDATA
        {
            public IWRMagSensor mag_sensor;
            public IWRAccelSensor acc_sensor;
            public IWRGyroSensor gyro_sensor;
            public IWRLBGyroSensor lbgyro_sensor;
        }

        // iWear Tracking DLL Import
        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWROpenTracker")]
        public static extern long IWROpenTracker();

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRCloseTracker")]
        public static extern void IWRCloseTracker();

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRBeginCalibrate")]
        public static extern long IWRBeginCalibrate();

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWREndCalibrate")]
        public static extern void IWREndCalibrate(bool save);

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRGetTracking")]
        public static extern long IWRGetTracking(ref int yaw, ref int pitch, ref int roll);

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRGet6DTracking")]
        public static extern long IWRGet6DTracking(ref int yaw, ref int pitch, ref int roll, ref int x, ref int y, ref int z);
 
        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRGetSensorData")]
        public static extern long IWRGetSensorData(ref IWRSENSDATA sensorData);

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRSetFilterState")]
        public static extern void IWRSetFilterState(Boolean on);

        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRZeroSet")]
        public static extern void IWRZeroSet();
        
        [DllImport("iWearDrv.dll", SetLastError = false, EntryPoint = "IWRGetProductID")]
        public static extern int IWRGetProductID();


        //iWear Stereo DLL Import
        [DllImport("iWrstDrv.dll", EntryPoint = "IWRSTEREO_Open", SetLastError = true)]
        public static extern IntPtr IWRSTEREO_Open();

        [DllImport("iWrstDrv.dll", EntryPoint = "IWRSTEREO_SetStereo")]
        public static extern Boolean IWRSTEREO_SetStereo(IntPtr handle, Boolean enabled);

        [DllImport("iWrstDrv.dll", EntryPoint = "IWRSTEREO_SetLR")]
        public static extern Boolean IWRSTEREO_SetLR(IntPtr handle, Boolean eye);

        [DllImport("iWrstDrv.dll", EntryPoint = "IWRSTEREO_WaitForAck")]
        public static extern Byte IWRSTEREO_WaitForAck(IntPtr handle, Boolean eye);
    }
}
